﻿using System.Web;
using NHibernate;
using NHibernate.Cfg;

namespace annuairedotnet
{
    /// <summary>
    /// Description résumée de NHibernateUtils
    /// </summary>
    public sealed class NHibernateUtils
    {
        private static readonly ISessionFactory sessionFactory;

        static NHibernateUtils()
        {
            sessionFactory = new Configuration().Configure().BuildSessionFactory();
        }

        public static ISession getSession()
        {
            ISession currentSession = sessionFactory.OpenSession();

            return currentSession;
        }

        public static void closeSession()
        {
            ISession currentSession = sessionFactory.OpenSession();

            if (currentSession == null)
            {
                // No current session
                return;

            }

            currentSession.Close();
        }

        public static void closeSessionFactory()
        {
            if (sessionFactory != null)
            {
                sessionFactory.Close();
            }
        }
    }
}
